package com.ruoyi.server.api.favorites.service.imp;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.server.api.favorites.domain.Favorites;
import com.ruoyi.server.api.favorites.mapper.FavoritesMapper;
import com.ruoyi.server.api.favorites.service.FavoritesService;
import com.ruoyi.server.api.imgmanage.domain.ImgManage;
import com.ruoyi.server.api.imgmanage.mapper.ImgManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @ClassName FavoritesServiceImpl
 * @Description 点赞收藏业务层处理
 * @Author zsc
 * @Date 2024/10/22 16:19
 */
@Service
public class FavoritesServiceImpl implements FavoritesService {

    @Autowired
    private FavoritesMapper favoritesMapper;

    @Autowired
    private ImgManageMapper imgManageMapper;
    /**
    * @Author zsc
    * @Description 点赞图片存储或移除
    * @Date 2024/10/22 16:24
    * @Return boolean
    * @param favorites
    */
    @Override
    public AjaxResult saveOrDelUserImageLike(Favorites favorites) {
        if (favorites != null){
            if (favorites.isLiked()){
                favorites.setCreateTime(new Date());
                favorites.setUpdateTime(new Date());
              int i = favoritesMapper.saveUserImgeLike(favorites);
              return AjaxResult.success(i);
            }else {
              int i = favoritesMapper.delUserImgeLike(favorites);
              return AjaxResult.success(i);
            }
        }
        return AjaxResult.error("数据为空");
    }

    /**
    * @Author zsc
    * @Description 收藏图片存储或移除
    * @Date 2024/10/23 8:42
    * @Return com.ruoyi.common.core.domain.AjaxResult
    * @param favorites
    */
    @Override
    public AjaxResult saveOrDelUserImageCol(Favorites favorites) {
        if (favorites != null){
            if (favorites.isCol()){
                favorites.setCreateTime(new Date());
                favorites.setUpdateTime(new Date());
                int i = favoritesMapper.saveUserImgeCol(favorites);
                return AjaxResult.success(i);
            }else {
                int i = favoritesMapper.delUserImgeCol(favorites);
                return AjaxResult.success(i);
            }
        }
        return AjaxResult.error("数据为空");
    }
    /**
    * @Author zsc
    * @Description 根据用户id查询出点赞过的图片id
    * @Date 2024/10/23 8:51
    * @Return java.util.List<java.lang.Long>
    * @param userId
    */
    @Override
    public List<Long> getUserImageLikeById(BigInteger userId) {
        List<Long> ids = favoritesMapper.getUserImageLikeById(userId);
        return ids;
    }
    /**
    * @Author zsc
    * @Description 根据用户id查询出收藏过的图片id
    * @Date 2024/10/23 9:19
    * @Return java.util.List<java.lang.Long>
    * @param userId
    */
    @Override
    public List<Long> getUserImageCol(BigInteger userId) {
        List<Long> ids = favoritesMapper.getUserImageColById(userId);
        return ids;
    }
    
    /**
    * @Author zsc
    * @Description 根据ids获取所有图片点赞数量
    * @Date 2024/10/23 9:30
    * @Return java.util.Map<java.lang.Long,java.lang.Integer>
    * @param ids
    */
    @Override
    public List<Map<Long, Integer> > getLikeCounts(Long[] ids) {
      List<Map<Long, Integer> > likeCountsList = favoritesMapper.getLikeCounts(ids);
      return null;
    }

    /**
    * @Author zsc
    * @Description 根据ids获取所有img收藏数
    * @Date 2024/10/23 16:45
    * @Return java.util.Map<java.lang.Long,java.lang.Integer>
    * @param ids
    */
    @Override
    public Map<Long, Integer> getColCounts(Long[] ids) {
        return null;
    }

    /**
    * @Author zsc
    * @Description 根据id查询点赞列表
    * @Date 2024/10/23 16:45
    * @Return java.util.List<com.ruoyi.server.api.imgmanage.domain.ImgManage>
    * @param id
    */
    @Override
    public AjaxResult selectLikeImgById(BigInteger id) {
        //获取当前用户点赞过的图片id
        List<Long> ids = favoritesMapper.getUserImageLikeById(id);
        if (ids.size() == 0 ){
            return AjaxResult.error("您还没有点赞");
        }
        //根据ids获取图片数据
        List<ImgManage> imgManages = imgManageMapper.selectBatchImgByIds(ids);
        return AjaxResult.success(imgManages);
    }

    @Override
    public AjaxResult selectColImgById(BigInteger id) {
        //获取当前用户收藏过的图片id
        List<Long> ids = favoritesMapper.getUserImageColById(id);
        if (ids.size() == 0 ){
            return AjaxResult.error("您还没有收藏");
        }
        //根据ids获取图片数据
        List<ImgManage> imgManages = imgManageMapper.selectBatchImgByIds(ids);
        return AjaxResult.success(imgManages);
    }
}
